package com.wechat.scheme.jwt;

import com.wechat.scheme.entity.SchemeUser;
import com.wechat.scheme.exception.UnauthorizedException;
import com.wechat.scheme.repo.SchemeUserRepo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class JWTProvideSerivce {


    private static final long EXPIRE_TIME = 24*60*60*1000;

    public String secret = "wxscheme";

    @Autowired
    private SchemeUserRepo userRepo;

    public String createJWTToken(String userid,String username){
        long now = System.currentTimeMillis();//当前时间
        long exp = now + EXPIRE_TIME;//过期时间为1天
        return Jwts.builder()
                .setId(userid)
                .setSubject(username)
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS512,secret)
                .setExpiration(new Date(exp))
                .compact();

    }

    public boolean verifyJWTToken(String token){
        try {
            Claims claims =  Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
            return true;
        }catch (Exception e){
            log.error("Invalid JWT Toekn");
            throw new UnauthorizedException();
        }

    }

    public Authentication getAuthentication(String token) {
        Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        SchemeUser user = userRepo.findById(claims.getId()).get();
        return new UsernamePasswordAuthenticationToken(user,token);

    }
}